import 'package:bxultimate/app/pages/application/organization/organization_statistics_template_create/widgets/radio_from.dart';
import 'package:bxultimate/app/theme/app_theme.dart';
import 'package:bxultimate/app/theme/gaps.dart';
import 'package:bxultimate/domain/application/organization/OrganizationAnnouncementTemplateCreateDto.dart';
import 'package:bxultimate/domain/application/organization/OrganizationAnnouncementTemplateDto.dart';
import 'package:bxultimate/shared/util/util_mk.dart';
import 'package:bxultimate/widgets/app/app_title.dart';
import 'package:bxultimate/widgets/app/app_input.dart';
import 'package:bxultimate/widgets/mk_page.dart';
import 'package:bxultimate/widgets/mk_scroll_view.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart' hide TDInput, TDInputType;

import 'controller.dart';

class OrganizationStatisticsTaskFillPage extends StatefulWidget {
  const OrganizationStatisticsTaskFillPage({Key? key}) : super(key: key);

  @override
  State<OrganizationStatisticsTaskFillPage> createState() =>
      _OrganizationStatisticsTaskFillPageState();
}

class _OrganizationStatisticsTaskFillPageState
    extends State<OrganizationStatisticsTaskFillPage> {
  final controller = Get.find<OrganizationStatisticsTaskFillController>();
  final state = Get.find<OrganizationStatisticsTaskFillController>().state;

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: OrganizationStatisticsTaskFillController(),
        builder: (_)=>MkPage(
        child: Scaffold(
          backgroundColor: Colors.transparent,
          appBar: TDNavBar(
            backgroundColor: Colors.transparent,
            title: '${state.taskInfo.taskTitle}',
          ),
          body: MkScrollView(
            bottomButton: TDButton(
              text: '提交',
              isBlock: true,
              theme: TDButtonTheme.primary,
              onTap: controller.publish,
            ),
            padding: EdgeInsets.all(AppTheme.iDefaultPadding),
            children: [
              Text('${state.taskInfo.taskDescription}'),
              for(var i = 0; i<state.formItem.length;i++)
                _buildItem(i)
            ],
          ),
        )));
  }

  _buildItem(index) {
    OrganizationAnnouncementTemplateCreateDto dto = state.formItem[index];
    if (dto.type == 1 || dto.type == 2) {
      // 文本
      return TDInput(
        leftLabel: '${dto.textInput}',
        required: dto.required,
        type: TDInputType.special,
        onChanged: (val){
          controller.inputChange(index,val);
        },
        hintText: '${dto.textinputplaceholder}',
        inputType: dto.type == 1 ? TextInputType.text : TextInputType.number,
        textAlign: TextAlign.end,
        backgroundColor: Colors.transparent,
      );
    }
    if (dto.type == 3) {
      // 日期
      return TDCell(
        style: TDCellStyle(
          backgroundColor: Colors.transparent,
          requiredStyle: TextStyle(color: Colors.red),
        ),
        title: '${dto.textInput}',
        arrow: true,
        note: '${dto.value ?? ''}',
        required: dto.required,
        onClick: (v){
          controller.showTimePicker(index);
        },
      );
    }
    if (dto.type == 4) {
      // 单选
      return TDCell(
        style: TDCellStyle(
          backgroundColor: Colors.transparent,
          requiredStyle: TextStyle(color: Colors.red),
        ),
        title: '${dto.textInput}',
        arrow: true,
        note: '${dto.value ?? ''}',
        required: dto.required,
        onClick:  (v){
          controller.showRadioPicker(index);
        },
      );
    }
    return Container();
  }

  @override
  void dispose() {
    Get.delete<OrganizationStatisticsTaskFillController>();
    super.dispose();
  }
}
